import pickle
import sys



def openreadtxt(file_name):
    time = []
    voltage = []
    file = open(file_name, 'r')  # 打开文件
    file_data = file.readlines()  # 读取所有行
    for row in file_data:
        tmp_list = row.split(', ')  # 按' '切分每行的数据
        tmp_list[-1] = tmp_list[-1].replace('\n', '')  # 去掉换行符
        time.append([str(tmp_list[0])])
        voltage.append([float(tmp_list[1])])
    return time, voltage


def predict(model_path, predict_path, predict_result_path):
    # 打开模型
    with open(model_path, 'rb') as file:
        result = pickle.loads(file.read())
    time, voltage = openreadtxt(predict_path)
    predict = []
    for x in voltage:
        predict.append(result.eval(x)[0])
    # 得到预测结果的绝对路径
    # temp = predict_path.split('\\\\')
    # temp[-2] = 'predictResult'
    # temp[-1] = temp[-1][:-4] + '_result.txt'
    # predict_path = ''
    # for i in temp:
    #     predict_path += i + '\\'
    # predict_path = predict_path[:-1]
    predict_path = predict_result_path
    f = open(predict_path, 'w')  # 这里前面的路径改成项目的路径
    for i in range(len(time)):
        f.write(str(time[i][0]) + ', ' + str(round(predict[i], 6)) + '\n')
    f.close()
    print(predict_path)


if __name__ == '__main__':
    a = []
    for i in range(1, len(sys.argv)):
        a.append(sys.argv[i])
    predict(a[0], a[1], a[2])
    # predict('D:\\Java练习\\gep_test\\data\\models\\moxingming.pkl', 'D:\\Java练习\\gep_test\\data\\predictFile\\predict.txt')
